package com.qdlc.p2p.manage.action.additional;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.AdditionalUserDrawLotteryService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.model.AdditionalUserDrawLotteryModel;

/**
 * Created by liaoxiangkai on 2015/11/26.
 */
public class ManageUserDrawLotteryAction extends BaseAction<AdditionalUserDrawLotteryModel> implements ModelDriven<AdditionalUserDrawLotteryModel> {

    @Resource
    private AdditionalUserDrawLotteryService additionalUserDrawLotteryService;

    private AdditionalUserDrawLotteryModel model = new AdditionalUserDrawLotteryModel();

    public AdditionalUserDrawLotteryModel getModel() {
        return model;
    }

    private Map<String, Object> data;

    //正常
    private static final int USERLOTTERY_STATUS_ENABLE = 0;
    //已使用
    private static final int USERLOTTERY_STATUS_DISABLE = 1;
    //已过期
    private static final int USERLOTTERY_STATUS_EXPIRE = 2;
    //无效
    private static final int USERLOTTERY_STATUS_INVALID = 3;

    /**
     * -------------------用户抽奖机会---------------------------
     *  进入页面
     * @return
     */
    @Action("/modules/additional/drawLottery/userDrawLotteryManager")
    public String userDrawLotteryManager() {
        return "userDrawLotteryManager";
    }

    @Action("/modules/additional/drawLottery/userDrawLotteryList")
    public void userDrawLotteryList() throws IOException {
        data = new HashMap<String, Object>();
        String status = paramString("status");
        if (StringUtil.isBlank(status)) model.setStatus(99);

        PageDataList<AdditionalUserDrawLotteryModel> pageDataList = additionalUserDrawLotteryService.list(model);
        if (pageDataList == null) {
            return;
        }
        data.put("total", pageDataList.getPage().getTotal());
        data.put("rows", pageDataList.getList());
        printJson(getStringOfJpaObj(data));
    }



    /**
     * 将用户抽奖机会设置成无效，畜生啊。。
     * 状态：0:正常 1:已使用 2:已过期 3:无效
     *
     * @return
     */
    @Action("/modules/additional/drawLottery/doUserDrawLotteryInvalid")
    public void doUserDrawLotteryFailure() throws Exception {
        long userLotteryId = model.getId();
        if (userLotteryId <= 0) {
            printWebResult("该条用户抽奖机会没有查到！", false);
        }
        int result = additionalUserDrawLotteryService.doInvalid(ManageUserDrawLotteryAction.USERLOTTERY_STATUS_INVALID, userLotteryId);
        if (result == 0) {
            printWebResult("将用户设置抽奖机会设置为无效，操作失败！", false);
        } else {
            printSuccess();
        }
    }


}
